/*=========================================================================*//**
*
*
*//*===========================================================================*/
//==============================================================================
//	IMPORT
//==============================================================================
#import "libReachability.h"

static libReachability* g_instance = nil;

@interface libReachability(private)
- (void)InternetReachabilityChanged:(NSNotification *)note;
- (void)InternetConnectCheck;
- (void)WiFiReachabilityChanged:(NSNotification *)note;
- (void)WiFiConnectCheck;
@end

@implementation libReachability


- (id)init
{
	if(self = [super init]){
		m_IsInternetConnect = NO;
		m_IsWiFiConnect = NO;
		
		m_InternetReach = [[Reachability reachabilityForInternetConnection] retain];
		[m_InternetReach startNotifer];
		
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(InternetReachabilityChanged:) name:kReachabilityChangedNotification object:nil];
		[self InternetConnectCheck];
	

	}
	
	return self;
}



- (void)dealloc
{
	SAFE_RELEASE(m_InternetReach);
///	SAFE_RELEASE(m_WiFiReach);
	
	[super dealloc];
}


+ (void)setSharedReachability:(libReachability*)reach
{
	[g_instance autorelease];
	g_instance = [reach retain];
}


+ (libReachability*)Shared
{
	if ( !g_instance ) {
		g_instance = [[libReachability alloc] init];
	}
	return g_instance;
}


- (void)InternetReachabilityChanged:(NSNotification *)note
{
	[self InternetConnectCheck];
}


- (void)InternetConnectCheck
{
	NetworkStatus status = [m_InternetReach currentReachabilityStatus];
	
	if( status == NotReachable )
	{
		
		m_IsInternetConnect = NO;
		m_IsWiFiConnect = NO;
	}
	else
	if ( status == ReachableViaWWAN )
	{
		
		m_IsInternetConnect = YES;
		m_IsWiFiConnect = NO;
	}
	else
	if ( status == ReachableViaWiFi )
	{
		
		m_IsInternetConnect = YES;
		m_IsWiFiConnect = YES;
	}
}


- (void)WiFiReachabilityChanged:(NSNotification *)note
{
	[self WiFiConnectCheck];
}


- (void)WiFiConnectCheck
{
	NetworkStatus status = [m_WiFiReach currentReachabilityStatus];
	
	if( status == NotReachable )
	{
		
		m_IsWiFiConnect = NO;
	}
	else
	{
		
		m_IsWiFiConnect = YES;
	}
}


- (BOOL)IsInternetConnect
{
	return m_IsInternetConnect;
}


- (BOOL)IsWiFiConnect
{
	return m_IsWiFiConnect;
}


- (BOOL)Is3GConnect
{
	return (m_IsInternetConnect && !m_IsWiFiConnect);
}

@end 
